// bindPhone.js
var app = getApp();
var common = require("../../common/utils.js");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    second: 29,
    code: true,
    appColor: app.appColor
  },
  phone: function(e) {
    this.setData({
      phone: e.detail.value
    })
  },
  codes: function() {
    var that = this;
    if (common.ChkUtil.isMobile(this.data.phone) == true) {

      // 验证码
      wx.request({
        url: app.serviceurl + 'Member/SendValidateNumber',
        header: {
          Authorization: "Bearer " + app.accesstoken
        },
        data: {
          PhoneNumber: this.data.phone
        },
        dataType: 'json',
        success: function(res) {
          if (res.data.Result == false) {
            if (res.data.Message == '新号码与原号码相同') {
              app.IsRegister = true;
            }
            that.alerts(res.data.Message);
            that.setData({
              code: true
            });
          } else {

            // 倒计时
            var seconds = that.data.second;
            that.setData({
              seconds: 30,
              code: false
            });
            var timer = setInterval(function() {
              if (seconds <= 0) {
                clearInterval(timer);
                that.setData({
                  code: true
                });
                return
              };
              that.setData({
                seconds: seconds--
              })
            }, 1000)
          }
        },
        fail: function(res) {
          console.log(res)
        }
      })
    } else {
      wx.showModal({
        content: '请确认这是正确的手机号',
        showCancel: false,
        success: function(res) {
          if (res.confirm) {
            console.log('确定')
          }
        }
      });
      return
    }
  },

  // 提示框
  alerts: function(message) {
    wx.showModal({
      content: message,
      showCancel: false,
      success: function(res) {
        if (res.confirm) {
          console.log('确定')
        }
      }
    });
  },

  // 绑定/更新手机号
  formSubmit: function(e) {
    var that = this,
      phones = e.detail.value.phones,
      verify = e.detail.value.verify;
    var that = this;
    if (phones && verify) {
      wx.request({
        url: app.serviceurl + 'Member/UpdateHandsetNumber',
        header: {
          Authorization: "Bearer " + app.accesstoken
        },
        data: {
          PhoneNumber: phones,
          ValidateCode: verify
        },
        dataType: 'json',
        success: function(res) {
          if (res.data.Result == false) {
            if (res.data.Message == "该号码未发送验证码" || res.data.Message == "无效验证码" || res.data.Message == "验证码无效") {
              that.alerts(res.data.Message);
              return
            }

            that.alerts(res.data.Message);
            that.setData({
              code: true
            });
            return
          } else {
            app.IsRegister = true;
            app.HandsetNumber = phones;
            that.setData({
              code: true
            });
            clearInterval(that.data.timer);
            wx.showToast({
              title: '绑定成功',
              icon: 'success',
              duration: 800
            });
            wx.navigateBack({});
          }
        },
        fail: function(res) {
          console.log(res)
        }
      })
    } else {
      wx.showModal({
        content: '请核对手机号或验证码是否正确',
        showCancel: false,
        success: function(res) {
          if (res.confirm) {
            console.log('确定')
          }
        }
      });
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  // 手机绑定模态框
  // 微信授权绑定
  getPhoneNumber: function(e) {
    if (!app.accesstoken) {
      wx.switchTab({
        url: '/pages/index/index',
      })
    }
    var that = this;
    if (e.detail.encryptedData != '' && e.detail.iv != '') {
      wx.request({
        url: app.serviceurl + 'Member/WxHandsetBinding',
        header: {
          Authorization: "Bearer " + app.accesstoken
        },
        data: {
          EncryptedData: e.detail.encryptedData,
          IV: e.detail.iv,
          Code: app.codes
        },
        dataType: 'json',
        success: function(res) {
          if (!res.data.Result) {
            if (res.data.Message == '已绑定手机号，无需重复绑定') {
              app.IsRegister = true;
            }
            //  提示框
            wx.showModal({
              content: res.data.Message,
              showCancel: false,
              success: function(res) {
                if (res.confirm) {
                  wx.navigateBack({});
                }
              }
            });
            return
          }
          app.IsRegister = true;
          app.HandsetNumber = res.data.HandsetNumber;
          // that.data.IsRegister = true;
          // that.setData({
          // HandsetNumber: res.data.HandsetNumber,
          // IsRegister: true
          // })
          wx.showModal({
            content: '绑定成功',
            showCancel: false,
            success: function(res) {
              if (res.confirm) {
                wx.navigateBack({})
              }
            }
          });
        },
        fail: function() {
          console.log("调用失败");
        }
      })
    } else {
      wx.showModal({
        content: '抱歉，您的微信暂未绑定手机号，请使用短信验证码绑定',
        showCancel: false,
        success: function(res) {
          if (res.confirm) {}
        }
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {
    clearInterval(this.data.timer);

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    clearInterval(this.data.timer);

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },
})